*********************************************************************************
*                                                                               *
*                              Analysis 3 										*
*                                                                               *
*                     		    Analysis       									*
*                                                                               *
*********************************************************************************

*global beg_path `"C:\Users\JohnSmith\Dropbox\Replication_EF\"'   
 
use `"${beg_path}Data\Analysis 3\cses_imd.dta"', clear


recode IMD2001_1 (9997 9998 9999 = .), gen(age) 
recode IMD1013_Y (9999=.), gen(year_interview)
gen yob = year_interview-age
lab var yob "Year of birth"
recode IMD3001 (9999993/9999999=.), gen(turnout) 
encode IMD1004, gen(election_study)
recode IMD5051_1 (99=.), gen(polity)

encode IMD1006_NAM, gen(Country_CSES)
fre Country_CSES, all
gen year = IMD1008_YEAR // Election year

tabstat turnout, by(election_study) 
drop if inlist(election_study, 144, 168) // RUS_2000  URY_2009 (reported turnout 100%)

gen date = mdy(IMD1011_M, IMD1011_D, IMD1011_Y)

merge m:1 Country_CSES date using `"${beg_path}Data\Analysis 3\cses_fre_to_merge.dta"'
keep if _merge==3
drop _merge

********************************************************************************
************************* Preparing the Data ***********************************
********************************************************************************

*Merges to add Contact
rename (IMD1003  IMD1004 IMD1005) (study_id_num study_id_alp respondent_id)

merge 1:1 study_id_alp respondent_id using `"${beg_path}Data\Analysis 3\cses2_stata\cses2_to_merge.dta"'
drop if _merge==2
drop _merge 

merge 1:1 study_id_alp respondent_id using `"${beg_path}Data\Analysis 3\cses4_stata\cses4_to_merge.dta"'
drop if _merge==2
drop _merge 

fre B3003 D3019

*CSES 2 : B3003
recode B3003 (1=1 "Yes") (2=0 "No") (else = 2 "missing"), gen(contact) 

*CSES 4 : B3003
replace contact = 0 if inlist(D3019, 5)
replace contact = 1 if inlist(D3019, 1)
fre IMD2003 

* Controls 
recode IMD2002 (1=0 "Male") (2=1 "Female") (else=.), gen(female)
recode IMD2003 (5/9 = .), gen(education) 
tab education, gen(educ)
recode IMD2006 (7/9 = .), gen(income) 
tab income, gen(inc)
tab election_study, gen(elec)
gen age_sq = age*age
recode IMD3005_1 (1=1 "Close") (2/8=0 "No") (else=.), gen(party_close)
recode IMD2007 (7/9 =.), gen(city)
tab city, gen(cit)
sort Country_CSES Year
recode IMD3012 (8=3) (7 9 =.), gen(efficacy)

*Reported turnout
gen temp_weight = IMD1010_1*IMD1010_2
gen rep_turnout=.
levels study_id_num, local(election_year)
foreach i of numlist `election_year' {
cap tab turnout [aw=temp_weight] if study_id_num ==`i', matcell(x) 
matrix list x
cap replace rep_turnout = x[2,1]/(x[2,1]+x[1,1]) if study_id_num ==`i'
}

*Real Turnout
gen real_turnout = Turnout/100

*Weight
gen weight_turnout = . 
levels study_id_num, local(election_year)
foreach i of numlist `election_year'  {
capture noisily display "Election" `i' 
replace weight_turnout=real_turnout/rep_turnout if study_id_num ==`i' & turnout==1
replace weight_turnout=(1-real_turnout)/(1-rep_turnout) if study_id_num ==`i' & turnout==0 
}
fre weight_turnout
gen final_weight=weight_turnout*temp_weight  

*Test 
tab turnout [aw=final_weight] if study_id_num == 75202006 
tab real_turnout  if study_id_num == 75202006  

*Filter for filtering out observations with missing values
gen filter = female!=. & age!=. & education!=. & income !=. & city!=. & party!=. & efficacy!=.

********************************************************************************
********************************** Analyses ************************************
********************************************************************************

***********
* Table 4 *
***********

*Analysis
cd `"${beg_path}Results"'
tabstat index_n_w_5, by( study_id_alp)
eststo clear

eststo M1: melogit turnout c.index_n_w_5 i.Country_CSES [pw=final_weight] if filter==1 || study_id_alp: , 
matrix x = e(N_g)
estadd scalar N_years= x[1,1] // 
estadd local fixed "Yes" , replace 
estadd local macro_controls "No", replace
estadd local micro_controls "No", replace

eststo M2: melogit turnout c.index_n_w_5 Majority Closeness ib2.Elsystem    Joint1 snap i.Country_CSES [pw=final_weight] if filter==1 || study_id_alp:  , 
matrix x = e(N_g)
estadd scalar N_years= x[1,1] // 
estadd local fixed "Yes" , replace 
estadd local macro_controls "Yes", replace
estadd local micro_controls "No", replace

eststo M3: melogit turnout c.index_n_w_5 Majority Closeness ib2.Elsystem   Joint1 snap c.age##c.age female educ2-educ5 inc2-inc5 cit2-cit4 party efficacy i.Country_CSES [pw=final_weight] if filter==1 || study_id_alp:  , 
matrix x = e(N_g)
estadd scalar N_years= x[1,1] // 
estadd local fixed "Yes" , replace 
estadd local macro_controls "Yes", replace
estadd local micro_controls "Yes", replace

*For interpretation in the text
margins, dydx(index_n_w_5) 
su index_n_w_5 if contact!=2 & filter==1
di  -.0535799 * r(sd)

eststo M4: melogit turnout c.index_n_w_5 i.contact Majority Closeness ib2.Elsystem   Joint1 snap  c.age##c.age female educ2-educ5  inc2-inc5 cit2-cit4 party efficacy i.Country_CSES  [pw=final_weight] if filter==1 || study_id_alp:, 
matrix x = e(N_g) 
estadd scalar N_years= x[1,1] // 
estadd local fixed "Yes" , replace 
estadd local macro_controls "Yes", replace
estadd local micro_controls "Yes", replace

eststo M5: melogit turnout c.index_n_w_5##i.contact Majority Closeness ib2.Elsystem  Joint1 snap c.age##c.age female educ2-educ5  inc2-inc5 cit2-cit4 party efficacy i.Country_CSES  [pw=final_weight] if filter==1 || study_id_alp: ,
matrix x = e(N_g) 
estadd scalar N_years= x[1,1] // 
estadd local fixed "Yes" , replace 
estadd local macro_controls "Yes", replace
estadd local micro_controls "Yes", replace

cap set scheme bw // Setting the scheme, not necessary for running the script

margins, dydx(contact) at(index_n_w_5=(.0(.2)2.6)) 

marginsplot, level(95) yline(0, lpattern(dash)) recast(line) recastci(rline)  xtitle("Index of Election Frequency (IEF)", margin(small)) ///
		 addplot(hist index_n_w_5 if contact!=., percent bin(7)  yaxis(2) yscale(axis(2) alt) xlabel(0(.2)2.6, grid) ylabel(-0.12(0.02)0.12) /// 	
		 yscale(range(0(10)100) axis(2)) ylabel(0(10)30, axis(2)) ///
		 ytitle("Observations (%)", axis(2) margin(medsmall)))  plot2opts(lc(none)) ci2opts(lc(none))  ///  
		ytitle(" " "AME Contact", height(0) axis(1))  legend(off) title("") name(graph1, replace) scheme(bw)
		
graph export Figure_6.eps, replace 
 
esttab M1 M2 M3 M4 M5 using Table_4.tex, stats(macro_controls micro_controls fixed N N_years, labels("Macro-level controls" "Individual-level controls" "Country FE" "N (individuals)" "N (elections)") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{$@$}" "\multicolumn{1}{c}{$@$}") f(0 0)) one incelldelimite("\:") replace b(2) se(2) wide staraux ///  that goes in labels
	star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  drop(0.*  *.Country_CSES  Majority Closeness *.Elsystem   Joint1 snap  age c.age#c.age female educ* inc* cit* party_close efficacy) eqlabels(none) nodep  ///
	order( index* 1.contact#c.index*   1.contact 2.contact#c.index* 2.contact   _cons  var(_cons[study_id_alp])) ///  
 nolegend coeflabels(index_n_w_5  "IEF" 1.contact#c.index_n_w_5 "IEF X Contact-Yes" /// 
 1.contact "Contact-Yes" 2.contact#c.index_n_w_5 "IEF X Contact-NA"  2.contact "Contact-NA" _cons "Constant" ///
var(_cons[study_id_alp]) "Country-Year Variance", elist(_cons  \hline))  ///
varwidth(24) nolegend note("") nomtitles alignment(d{10}) //  

*************
* Table A14 *
*************
*Full Table

esttab M1 M2 M3 M4 M5 using A_Table_14.tex, stats(fixed N N_years, labels("Country FE" "N (individuals)" "N (elections)") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{$@$}" "\multicolumn{1}{c}{$@$}") f(0 0)) one incelldelimite("\:") replace b(2) se(2) wide staraux ///  that goes in labels
	star(* 0.05 ** 0.01 *** 0.001)  drop(  *.Country_CSES  )   eqlabels(none) nodep nobase ///
	order( index* 1.contact#c.index*   1.contact 2.contact#c.index* 2.contact Majority Closeness 1.Elsystem 3.Elsystem ///
	Joint1 snap age c.age#c.age female educ* inc* cit* party_close efficacy _cons  var(_cons[study_id_alp])) /// 
 nolegend coeflabels(index_n_w_5  "IEF" 1.contact#c.index_n_w_5 "IEF X Contact-Yes" /// 
 1.contact "Contact-Yes" 2.contact#c.index_n_w_5 "IEF X Contact-NA"  2.contact "Contact-NA" 1.Elsystem /// 
 "Majoritarian" 3.Elsystem "Mixed" Joint1 "Concurrent Elections" snap "Snap Election" age "Age" c.age#c.age "Age^2" female "Female" ///
 educ2 "Education: Primary" educ3 "Education: Secondary" educ4 "Education: Post-Secondary"  educ5 "Education: University" /// 
 inc2 "Income: Quintile 2" inc3 "Income: Quintile 3" inc4 "Income: Quintile 4" inc5 "Income: Quintile 5" ///
 cit2 "Small/Mid-Sized Town"  cit3 "Suburb of a Large City" cit4 "Large City"  party_close "Close to a Party" efficacy /// 
 "Efficacy" _cons "Constant" ///
var(_cons[study_id_alp]) "Country-Year Variance", elist(_cons \hline))  ///
varwidth(24) nolegend note("") nomtitles  alignment(d{10})
 
 
*************
* Table A15 *
************* 

***Robustness Checks
tabstat index_n_w_5 if contact, by(Country) // Largest index_n_w_5 - Slovenia, smallest index_n_w_5 - Norway

eststo clear
*Without France
eststo M1: melogit turnout c.index_n_w_5##i.contact  Majority Closeness ib2.Elsystem  Joint1 snap c.age c.age##c.age  female educ2-educ5 inc2-inc5 cit2-cit4 party efficacy i.Country_CSES [pw=final_weight] if Country_CSES !=16 & filter==1 || study_id_alp: 
matrix x = e(N_g)
estadd scalar N_years= x[1,1] // 
estadd local fixed "Yes" , replace 

*Without Norway
eststo M2: melogit turnout c.index_n_w_5##i.contact  Majority Closeness ib2.Elsystem  Joint1 snap c.age c.age##c.age female educ2-educ5 inc2-inc5 cit2-cit4 party efficacy i.Country_CSES [pw=final_weight] if Country_CSES !=35 & filter==1 || study_id_alp:  
matrix x = e(N_g)
estadd scalar N_years= x[1,1] // 
estadd local fixed "Yes" , replace 

*Only for non-missing contact
eststo M3: melogit turnout c.index_n_w_5##i.contact  Majority Closeness ib2.Elsystem  Joint1 snap c.age c.age##c.age female educ2-educ5 inc2-inc5 cit2-cit4 party efficacy i.Country_CSES [pw=final_weight] if contact!=2 & filter==1 || study_id_alp:  
matrix x = e(N_g)
estadd scalar N_years= x[1,1] // 
estadd local fixed "Yes" , replace 
 
*With random slopes
eststo M4: melogit turnout c.index_n_w_5##i.contact  Majority Closeness ib2.Elsystem  Joint1 snap  c.age c.age##c.age female educ2-educ5  inc2-inc5 cit2-cit4 party efficacy i.Country_CSES [pw=weight_turnout] if   filter==1 & contact!=2 || study_id_alp: i.contact, 
matrix x = e(N_g)
estadd scalar N_years= x[1,1] // 
estadd local fixed "Yes" , replace 
 
esttab M1 M2 M3 M4 using A_Table_15.tex, stats(fixed N N_years, labels("Country FE" "N (individuals)" "N (elections)") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{$@$}" "\multicolumn{1}{c}{$@$}") f(0 0)) one incelldelimite("\:")  replace b(2) se(2) wide staraux /// 
	star(* 0.05 ** 0.01 *** 0.001)  drop(*.Country_CSES)   eqlabels(none) nobase nodep  ///
	order( index* 1.contact#c.index*   1.contact 2.contact#c.index* 2.contact Majority Closeness 1.Elsystem 3.Elsystem ///
	Joint1 snap age c.age#c.age female educ* inc* cit* party_close efficacy _cons  var(_cons[study_id_alp]) var(1.contact[study_id_alp]) ) ///  
 nolegend coeflabels(index_n_w_5  "IEF" 1.contact#c.index_n_w_5 "IEF X Contact: Yes" /// 
 1.contact "Contact: Yes" 2.contact#c.index_n_w_5 "IEF X Contact: NA"  2.contact "Contact: NA" 1.Elsystem /// 
 "Majoritarian" 3.Elsystem "Mixed" Joint1 "Concurrent Elections" snap "Snap Election"  age "Age" c.age#c.age "Age^2" female "Female" ///
 educ2 "Education: Primary" educ3 "Education: Secondary" educ4 "Education: Post-Secondary"  educ5 "Education: University" /// 
 inc2 "Income: Quintile 2" inc3 "Income: Quintile 3" inc4 "Income: Quintile 4" inc5 "Income: Quintile 5" ///
 cit2 "Small/Mid-Sized Town"  cit3 "Suburb of a Large City" cit4 "Large City"  party_close "Close to a Party" efficacy "Efficacy" _cons "Constant" ///
var(_cons[study_id_alp]) "Country-Year Variance" var(1.contact[study_id_alp]) "Contact Variance" , elist(_cons  \hline))  ///
varwidth(24) nolegend note("") mtitles( "\shortstack{\\W/t France}" "\shortstack{\\W/t Norway}" "\shortstack{\\W/t Contact: NA}" "\shortstack{\\Random Slope}") alignment(d{10})

*************
* Table A13 *
************* 

*Desriptive statistics (Table)
tab contact, gen(conti)
tab Elsystem, gen(elsys)
lab var Joint1 "Concurrent Elections"
lab var elsys1 "Majoritarian"
lab var elsys2 "Proportional"
lab var elsys3 "Mixed"
lab var snap "Snap Election"
lab var  turnout "Turnout" 
lab var  index_n_w_5  "IEF" 
lab var  conti1 "Contact: No"
lab var  conti2 "Contact: Yes"
lab var  conti3 "Contact: NA"
lab var  age "Age" 
lab var  female "Female" 
lab var educ1 "No Education" 
lab var educ2 "Education: Primary" 
lab var educ3 "Education: Secondary" 
lab var educ4 "Education: Post-Secondary" 
lab var educ5 "Education: University" 
lab var inc1 "Income: Quintile 1" 
lab var inc2 "Income: Quintile 2" 
lab var inc3 "Income: Quintile 3" 
lab var inc4 "Income: Quintile 4" 
lab var inc5 "Income: Quintile 5" 
lab var cit1 "Rural Area or Village"
lab var cit2 "Small/Mid-Sized Town"  
lab var cit3 "Suburb of a Large City" 
lab var cit4 "Large City"  
lab var party_close "Close to a Party" 
lab var efficacy "Efficacy"
 
estpost tabstat turnout index_n_w_5 conti1 conti2 conti3  Majority Closeness  elsys1  elsys2  elsys3 Joint1 snap age female educ1 educ2 educ3 educ4 educ5 inc1 inc2 inc3 inc4 inc5 cit1 cit2 cit3 cit4 party_close efficacy [aw=final_weight] if filter==1 , stat(count mean sd min max ) c(s)   // format(%9.2f) labelwidth(32)

esttab . using A_Table_13.tex, replace cells("count(fmt(%9.0f)) mean(fmt(%9.2f)) sd(fmt(%9.2f)) min(fmt(%9.2f)) max(fmt(%9.2f))"  ) label varwidth(30) noobs ///
collabels("N" "Mean" "SD" "Min" "Max") nodep nonumb nomti modelwidth(6)  
	
*************
* Table A16 *
************* 

eststo clear		
eststo M1: melogit contact c.index_n_w_5 i.Country_CSES [pw=final_weight] if filter==1 & contact!=2 || study_id_alp: , 
matrix x = e(N_g)
estadd scalar N_years= x[1,1] // 
estadd local fixed "Yes" , replace 
estadd local macro_controls "No", replace
estadd local micro_controls "No", replace

eststo M2: melogit contact c.index_n_w_5 Majority Closeness ib2.Elsystem    Joint1 snap i.Country_CSES [pw=final_weight] if filter==1 & contact!=2  || study_id_alp:  , 
matrix x = e(N_g)
estadd scalar N_years= x[1,1] // 
estadd local fixed "Yes" , replace 
estadd local macro_controls "Yes", replace
estadd local micro_controls "No", replace

eststo M3: melogit contact c.index_n_w_5 Majority Closeness ib2.Elsystem   Joint1 snap c.age##c.age female educ2-educ5 inc2-inc5 cit2-cit4 party efficacy i.Country_CSES [pw=final_weight] if filter==1 & contact!=2  || study_id_alp:  , 
matrix x = e(N_g)
estadd scalar N_years= x[1,1] // 
estadd local fixed "Yes" , replace 
estadd local macro_controls "Yes", replace
estadd local micro_controls "Yes", replace

esttab M1 M2 M3  using A_Table_16.tex, stats(fixed N N_years, labels("Country FE" "N (individuals)" "N (elections)") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{$@$}" "\multicolumn{1}{c}{$@$}") f(0 0)) one incelldelimite("\:")  replace b(2) se(2) wide staraux /// 
	star(* 0.05 ** 0.01 *** 0.001)  drop(*.Country_CSES)   eqlabels(none) nobase nodep  ///
	order( index* Majority Closeness  3.Elsystem ///
	Joint1 snap age c.age#c.age female educ* inc* cit* party_close efficacy _cons  var(_cons[study_id_alp]) ) ///  
 nolegend coeflabels(index_n_w_5  "IEF"  ///
 3.Elsystem "Mixed" Joint1 "Concurrent Elections" snap "Snap Election"  age "Age" c.age#c.age "Age^2" female "Female" ///
 educ2 "Education: Primary" educ3 "Education: Secondary" educ4 "Education: Post-Secondary"  educ5 "Education: University" /// 
 inc2 "Income: Quintile 2" inc3 "Income: Quintile 3" inc4 "Income: Quintile 4" inc5 "Income: Quintile 5" ///
 cit2 "Small/Mid-Sized Town"  cit3 "Suburb of a Large City" cit4 "Large City"  party_close "Close to a Party" efficacy "Efficacy" _cons "Constant" ///
var(_cons[study_id_alp]) "Country-Year Variance", elist(_cons  \hline))  ///
varwidth(24) nolegend note("") nomtitles alignment(d{10})

*For interpretation below the table 
margins, dydx(index_n_w_5) 
su index_n_w_5 if contact!=2 & filter==1
di .0170063*r(sd)
		
		
		
		
		